<?php

namespace App\Workerman;

use App\Common\Logs\Logs;
use App\Common\Utill\Utills;
use Illuminate\Support\Facades\DB;

class Events
{

    public static function onWorkerStart($worker)
    {
        echo 'workman进程启动,进程id ' . $worker->id . PHP_EOL;

        //监听redis队列
        $redis = Utills::redisObj();
        while (true) {
            //读取redis队列
            $data = $redis->lPop('test-queue');
            if ($data) {
                //处理业务
                echo '进程id ' . $worker->id . ' 开始处理业务数据' . $data . PHP_EOL;
                //Logs::crontabLog('进程id ' . $worker->id . ' 开始处理业务数据' . $data,[],"workman");
                //模拟耗时任务
                sleep(5);
                $postData = [];
                $postData['cate_id'] = $worker->id;
                $postData['content'] = '进程id ' . $worker->id . ' 开始处理业务数据' . $data;
                $postData['created_at'] = date('Y-m-d H:i:s');
                DB::table("pd_xiancheng_test")->insert($postData);
                // echo '进程id ' . $worker->id . ' 处理业务数据' . $data . ' 完成' . PHP_EOL;
            } else {
                echo '进程id ' . $worker->id . ' 空闲中，休息5秒' . PHP_EOL;
                sleep(5);
            }
        }
    }

    public static function onConnect($client_id)
    {
    }

    public static function onWebSocketConnect($client_id, $data)
    {
    }

    public static function onMessage($client_id, $message)
    {
    }

    public static function onClose($client_id)
    {
    }
}
